<template>
  <div class="ma-content-block lg:flex justify-between">
    <!-- CRUD 组件 -->
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :sm="8" :xs="24">
          <a-form-item field="uuid" label="用户ID筛选">
            <a-input v-model="searchForm.uuid" placeholder="请输入用户ID" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item field="user_keyword" label="用户信息搜索">
            <a-input v-model="searchForm.user_keyword" placeholder="请输入用户手机号/昵称" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item field="name" label="姓名搜索">
            <a-input v-model="searchForm.name" placeholder="请输入姓名" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item field="community_name" label="社区名称搜索">
            <a-input v-model="searchForm.community_name" placeholder="请输入社区名称" allow-clear />
          </a-form-item>
        </a-col>
        <!-- <a-col :sm="8" :xs="24">
          <a-form-item field="status" label="状态">
            <sa-select v-model="searchForm.status" dict="data_status" placeholder="请选择状态" />
          </a-form-item>
        </a-col> -->
        
      </template>

      <!-- Table 自定义渲染 -->
      <!-- 状态列 -->
       <template #id="{ record }">
        {{ record.uuid }}/{{ record.phone }}/{{ record.nickname }}
      </template>
      <template #form_info="{ record }">
        <div>ID:{{ record.inviter_user?.id }}</div>
        <div>昵称:{{ record.inviter_user?.nickname }}</div>
      </template>
      <template #address="{ record }">
        {{ record.province }}{{ record.city }}{{ record.district }}{{ record.address }}
      </template>
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus($event, record.id)"></sa-switch>
      </template>
      <template #operationAfterExtend="{ record }">
        <!-- <a-link type="primary" @click="edit(record)"><icon-edit />编辑</a-link> -->
        <a-popconfirm
          content="确定要通过审核吗?"
          position="bottom"
          @ok="passAction(record)">
          <a-button type="primary">
            通过
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          content="确定要拒绝审核吗?"
          position="bottom"
          @ok="refuseAction(record)">
          <a-button type="primary">
            拒绝
          </a-button>
        </a-popconfirm>
      </template>
    </sa-table>

  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import api from '@/api/leader'

const crudRef = ref()
// 搜索表单
const searchForm = ref({
  name: '',
  uuid: '',
  phone: '',
  user_keyword: '',
  community_name: '',
  room_id: '',
})


// 修改状态
const changeStatus = async (status, id) => {
  const response = await api.changeStatus({ id, status })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

// SaTable 基础配置
const options = reactive({
  api: api.applyList,
  rowSelection: { showCheckedAll: true },
  shenhe: {
    show: true,
    text: '一键审核',
    func: async (e) => {
      const ids = e.ids.join(',')
      const response = await api.pass({ community_id: ids,status:1 })
      if (response.code === 200) {
        Message.success(response.message)
        crudRef.value.refresh()
      }
    },
  },
  delete: {
    show: false,
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '用户ID/手机号/昵称', dataIndex: 'id', width:220 },
  { title: '姓名', dataIndex: 'name', width: 100 },
  { title: '代理信息', dataIndex: 'form_info', width: 180 },
  { title: '社区名称', dataIndex: 'community_name', width: 180 },
  { title: '门头照片', dataIndex: 'image', type: 'image', width: 150 },
  { title: '门店地址', dataIndex: 'address', width: 220 },
  // { title: '状态', dataIndex: 'status', type: 'dict', dict: 'data_status', width: 120 },
])

// 审核通过
const passAction = async (record) => {
  const response = await api.pass({ community_id: record.id,status: 1 })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

// 审核拒绝
const refuseAction = async (record) => {
  const response = await api.pass({ community_id: record.id,status: 3 })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
